﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Administrator
{
    public partial class ucJournal : UserControl
    {
        public ucJournal()
        {
            InitializeComponent();
        }

        public void FillFields()
        {
            dtpStartDT.Value = fMain.JornalStartDT;
            dtpEndDT.Value = fMain.JornalEndDT;

            LoadJournal(fMain.JornalStartDT, fMain.JornalEndDT);
        }
        //Загрузка журнала из БД
        private void LoadJournal(DateTime StartDT, DateTime EndDT)
        {
            Int32 i;
            OleDbCommand DbCommand;
            OleDbDataAdapter DbDataAdapter;
            DataSet DbDataSet;
            ListViewItem ListItem = null;

            Cursor.Current = Cursors.WaitCursor;

            lvAddEvents.Visible = false;
            lvChangeEvents.Visible = false;
            lvDeleteEvents.Visible = false;
            lvAddEvents.Items.Clear();
            lvChangeEvents.Items.Clear();
            lvDeleteEvents.Items.Clear();

            if (cDbOracle.DbConnect != null)
            {
                try
                {                    
                    DbCommand = cDbOracle.DbConnect.CreateCommand();
                    DbDataAdapter = new OleDbDataAdapter(DbCommand);
                    DbDataSet = new DataSet();
                    //Загрузка событий
                    DbCommand.CommandText = "select * from AUDIT_KTS" +
                    " where IDGROUP=1" +
                    " and STAMP>=" + "to_date('" + StartDT.ToString() + "','dd.mm.yyyy hh24:mi:ss')" +
                    " and STAMP<=" + "to_date('" + EndDT.ToString() + "','dd.mm.yyyy hh24:mi:ss')" +
                    " order by STAMP";
                    DbDataAdapter.Fill(DbDataSet, "AUDIT_KTS");
                    foreach (DataRow DbDataRow in DbDataSet.Tables["AUDIT_KTS"].Rows)
                    {
                        ListItem = new ListViewItem();
                        ListItem.Text = (Convert.ToDateTime(DbDataRow["STAMP"].ToString())).ToString("dd.MM.yyyy HH:mm:ss");
                        ListItem.ImageIndex = -1;
                        ListItem.SubItems.Add(DbDataRow["DESCRIPTION"].ToString());
                        ListItem.SubItems.Add(DbDataRow["USER_NAME"].ToString());
                        for (i = 1; i < ListItem.SubItems.Count; i++)
                            ListItem.SubItems[i].ForeColor = SystemColors.GrayText;
                        if (Convert.ToInt32(DbDataRow["IDEVENT"].ToString()) == 1)
                            lvAddEvents.Items.Insert(0, ListItem);
                        if (Convert.ToInt32(DbDataRow["IDEVENT"].ToString()) == 2)
                            lvChangeEvents.Items.Insert(0, ListItem);
                        if (Convert.ToInt32(DbDataRow["IDEVENT"].ToString()) == 3)
                            lvDeleteEvents.Items.Insert(0, ListItem);
                    }                   
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, cConst.ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                ResizeColumns(lvAddEvents);
                ResizeColumns(lvChangeEvents);
                ResizeColumns(lvDeleteEvents);
                lvAddEvents.Visible = true;
                lvChangeEvents.Visible = true;
                lvDeleteEvents.Visible = true;
            }
            Cursor.Current = Cursors.Default;
        }
        //Изменение размеров столбцов
        private void ResizeColumns(ListView listView)
        {
            Int32 i, j;
            Double ColWidth, TextWidth;
            Graphics g = Graphics.FromHwnd(this.Handle);

            for (i = 0; i < listView.Columns.Count; i++)
            {
                ColWidth = 0;
                if (listView.Columns[i].Text != "")
                    ColWidth = g.MeasureString(listView.Columns[i].Text, listView.Font).Width;
                for (j = 0; j < listView.Items.Count; j++)
                {
                    TextWidth = 0;
                    if (listView.Items[j].SubItems.Count > i)
                    {
                        if (listView.Items[j].SubItems[i].Text != "")
                            TextWidth = g.MeasureString(listView.Items[j].SubItems[i].Text, listView.Font).Width;
                        if (TextWidth > ColWidth) ColWidth = TextWidth;
                    }
                }
                ColWidth = ColWidth + 15;
                listView.Columns[i].Width = (Int32)ColWidth;
            }
        }
        private void dtpStartDT_ValueChanged(object sender, EventArgs e)
        {
            if (sender == dtpStartDT) fMain.JornalStartDT = dtpStartDT.Value;
            if (sender == dtpEndDT) fMain.JornalEndDT = dtpEndDT.Value;
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (fMain.JornalStartDT > fMain.JornalEndDT)
                MessageBox.Show(cConst.STARTDT_GREATER_ENDDT, cConst.ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            LoadJournal(fMain.JornalStartDT, fMain.JornalEndDT);
        }
    }
}
